package com.siberiadante.multiscrolldemo.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewParent;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.siberiadante.multiscrolldemo.R;
import com.siberiadante.multiscrolldemo.adapter.MineQuestionAdapter;
import com.siberiadante.multiscrolldemo.bean.MineQuestionBean;
import com.siberiadante.multiscrolldemo.fragment.base.LazyFragment;
import com.siberiadante.multiscrolldemo.view.JudgeNestedScrollView;
import com.siberiadante.multiscrolldemo.view.NormalDecoration;


import java.util.ArrayList;
import java.util.List;


/**
 * @Created SiberiaDante
 * @Describe：
 * @CreateTime: 2017/12/14
 * @UpDateTime:
 * @Email: 2654828081@qq.com
 * @GitHub: https://github.com/SiberiaDante
 */

public class QuestionFragment extends LazyFragment {
    public static final String TAG = QuestionFragment.class.getSimpleName();

    RecyclerView recyclerView;
    SmartRefreshLayout smartRefreshLayout;
    private MineQuestionAdapter adapter;

    public static QuestionFragment getInstance() {
        return new QuestionFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_dynamic;
    }

    final List<MineQuestionBean> data = new ArrayList<>();

    private JudgeNestedScrollView scrollView;

    @Override
    public void lazyInitView(View view, Bundle savedInstanceState) {
        recyclerView = view.findViewById(R.id.recycler_view);
        smartRefreshLayout = view.findViewById(R.id.srl);

        for (int i = 0; i < 10; i++) {
            final MineQuestionBean questionBean = new MineQuestionBean();
            questionBean.setContent("使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种滑动冲突" + i);
            data.add(questionBean);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerView.addItemDecoration(new NormalDecoration(ContextCompat.getColor(mActivity, R.color.mainGrayF8),
                (int) mActivity.getResources().getDimension(R.dimen.eight)));

        adapter = new MineQuestionAdapter(mActivity);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(mActivity, "---position---" + position, Toast.LENGTH_SHORT).show();
            }
        });
        adapter.addAll(new ArrayList<>(data));
        smartRefreshLayout.setRefreshFooter(new ClassicsFooter(getContext()));

        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                if (adapter.getCount() > 20) {
                    refreshLayout.setNoMoreData(true);
                    refreshLayout.finishLoadMore(2000);
//                    refreshLayout.finishLoadMore(200, true, false);
                } else {
                    int count = adapter.getCount();
                    data.clear();
                    for (int i = 1; i <= 10; i++) {
                        final MineQuestionBean questionBean = new MineQuestionBean();
                        questionBean.setContent("使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种滑动冲突" +( i+count));
                        data.add(questionBean);
                    }
                    adapter.addAll(data);
                    refreshLayout.finishLoadMore(200);
                }
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                refreshLayout.setNoMoreData(false);
                data.clear();
                for (int i = 1; i <= 10; i++) {
                    final MineQuestionBean questionBean = new MineQuestionBean();
                    questionBean.setContent("打造酷炫下拉视差效果并解决各种滑动冲突" + i);
                    data.add(questionBean);
                }
                adapter.setNewData(data);
                refreshLayout.finishRefresh();
            }
        });
    }

    private void setData() {

    }

    @Override
    protected void initData() {

    }
}
